#include <sys/time.h>
#include <ctime>
#include <cstdio>
#include <cstdarg>
#include "logger.hpp"

namespace logger 
{
    // 默认各种级别的日志输出到哪个地方
    std::FILE* s_sinks[] = {
        stderr,  // PANIC
        stderr,  // ERROR
        stdout,  // WARNING
        stdout,  // INFO
        stdout   // DEBUG
    };

    // 每种级别下的日志输出样式，包含了颜色
    const char* s_patterns[] = {
        "\033[1;3;31m%s.%03ld PANIC %s@%d: %s\033[0m\n",
        "\033[1;31m%s.%03ld ERROR %s@%d: %s\033[0m\n",
        "\033[33m%s.%03ld WARN  %s@%d: %s\033[0m\n",
        "\033[0m%s.%03ld INFO  %s@%d: %s\033[0m\n",
        "\033[90m%s.%03ld DEBUG %s@%d: %s\033[0m\n"
    };
    
    // 默认的日志级别
    Severity s_severity = Severity::DEBUG;

    static void LoggingFunction(const char* file, int line, Severity severity, const char* log)
    {
        if (severity < Severity::PANIC || severity > Severity::DEBUG) {
            std::fprintf(stderr, "Invalid severity");
            return;
        }
        // 对于日志级别大于默认级别的忽略不打印
        if (s_severity == Severity::NONE || severity > s_severity) {
            return;
        }

        int severityIndex = static_cast<int>(severity);
        std::FILE* outStream = s_sinks[severityIndex];
        
        struct tm* tmInfo;
        struct timeval tv;
        gettimeofday(&tv, NULL);
        struct tm localTm;
        tmInfo = localtime_r(&tv.tv_sec, &localTm);
        char timeStr[20];
        strftime(timeStr, 20, "%Y-%m-%d %H:%M:%S", tmInfo);
        std::fprintf(outStream, s_patterns[severityIndex], timeStr, tv.tv_usec / 1000, file, line, log);
        std::fflush(outStream);
    }

    // 不定长参数函数
    void Log(const char* file, int line, Severity severity, const char* txt, ...)
    {
        va_list args1;
        va_start(args1, txt);               // args1指向txt参数后的那一个参数
        va_list args2;
        va_copy(args2, args1);
        int bufSize = std::vsnprintf(NULL, 0, txt, args1);  // 获取打印buffer需要的长度
        char* buffer = new char[bufSize + 1];               // vsnprintf只返回字符个数，不包含null
        std::vsnprintf(buffer, bufSize, txt, args2);        // 此时不能用args1了
        LoggingFunction(file, line, severity, buffer);      // 真正的打印函数
        va_end(args1);
        va_end(args2);
        delete [] buffer;
    }

    void Redirect(std::FILE* file, Severity severity)
    {
        if (severity == Severity::ALL) {
            for (int i = 0; i < static_cast<int>(Severity::COUNT); i++) {
                s_sinks[i] = file;
            }
            return;
        }
        int severityIndex = static_cast<int>(severity);
        s_sinks[severityIndex] = file;
    }

    void SetSeverity(Severity severity)
    {
        if (severity < Severity::PANIC || severity > Severity::DEBUG) {
            std::fprintf(stderr, "SetSeverity: invalid severity\n");
            return;
        }
        s_severity = severity;
    }
}